package com.wwj.springboot.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.wwj.springboot.entity.CmfzAdmin;
import com.wwj.springboot.service.CmfzAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @author wwj
 */
@Controller
@RequestMapping("login")
public class LoginController {


    @Autowired
    private CmfzAdminService cmfzAdminService;



    @RequestMapping("addAdmin")
    @ResponseBody
    public Map addAdmin(CmfzAdmin cmfzAdmin){
        return cmfzAdminService.addAdmin(cmfzAdmin);
    }


    @PostMapping("login")
    public String login(CmfzAdmin admin, String code, HttpSession session) throws Exception {

        String sessionAttribute = (String) session.getAttribute("code");
        if(!sessionAttribute.equalsIgnoreCase(code)){
            //验证码输入错误，返回注册页面
            String msg="验证码错误";
            return "redirect:/login.jsp?msg="+ URLEncoder.encode(msg,"utf-8");
        }


        //        1.封装到令牌中
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword());
        // 2.获取主体
        Subject subject = SecurityUtils.getSubject();

        // Session的使用和以前一样
        //   获取Session Shiro提供了一种更加简单的方法


        // 3.通过主体登录
        try {
            subject.login(token);
        //  获取用户名
            String adminUsername = SecurityUtils.getSubject().getPrincipal().toString();
            System.out.println(adminUsername);
            session.setAttribute("admin", admin);
            return "redirect:/main.jsp";
        }catch (Exception e){
            return "redirect:/login.jsp";
        }
    }


    /**
     * 集成Shiro之前是调用业务层login 集成shiro之后需要调用 主体的login方法
     */
    @RequestMapping("adminLogin")
    public String adminLogin(String username,String password){
//        1.封装到令牌中
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//        2.获取主体
        Subject subject = SecurityUtils.getSubject();

        //        Session的使用和以前一样
//        获取Session Shiro提供了一种更加简单的方法
        Session session = subject.getSession();

//        3.通过主体登录
        try {
            subject.login(token);
//            获取用户名
            String adminUsername = SecurityUtils.getSubject().getPrincipal().toString();
            System.out.println(adminUsername);
            return "redirect:/main.jsp";
        }catch (Exception e){
            return "redirect:/login.jsp";
        }
    }



    @RequestMapping("getCode")
    public void getCode(HttpServletResponse response, HttpSession session){

        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);

        //将验证码放入session
        System.out.println(captcha.getCode());
        session.setAttribute("code",captcha.getCode());

        try {
            ServletOutputStream outputStream = response.getOutputStream();
            captcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
